﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibraryDS;
using LibraryUS;
using LibraryDS.CDBNames;
using IPCommon;

namespace LibraryMain
{
    public partial class f009_V_GD_PHIEU_MUON_DE : Form
    {
        public f009_V_GD_PHIEU_MUON_DE()
        {
            InitializeComponent();
            format_controls();
        }

        #region Public Interface
        public void display()
        {
            this.ShowDialog();
        }
        public void display_for_update(US_V_GD_PHIEU_MUON ip_US_V_GD_PHIEU_DANG_KY)
        {
            m_us_v_gd_phieu_muon = ip_US_V_GD_PHIEU_DANG_KY;
            m_e_form_mode = DataEntryFormMode.UpdateDataState;
            this.ShowDialog();
        }
        public void display_for_insert()
        {
            m_e_form_mode = DataEntryFormMode.InsertDataState;
            this.ShowDialog();
        }

        #endregion

        #region Members
        DataEntryFormMode m_e_form_mode;
        US_V_GD_PHIEU_MUON m_us_v_gd_phieu_muon = new US_V_GD_PHIEU_MUON();
        US_GD_PHIEU_MUON m_us_gd_phieu_muon = new US_GD_PHIEU_MUON();
        #endregion

        #region Data Structure
        #endregion

        #region Private Method

        private void enable_combobox(bool ip_b)
        {
        }
        private void load_data_to_combobox()
        {
            US_V_GD_PHIEU_MUON v_us_gd_phieu_muon = new US_V_GD_PHIEU_MUON();
            DS_V_GD_PHIEU_MUON v_ds_gd_phieu_muon = new DS_V_GD_PHIEU_MUON();
            v_us_gd_phieu_muon.FillDataset(v_ds_gd_phieu_muon);
            m_cbo_ma_doc_gia.DataSource = v_ds_gd_phieu_muon.V_GD_PHIEU_MUON;
            m_cbo_ma_doc_gia.DisplayMember = V_GD_PHIEU_MUON.MA_DOC_GIA;
            m_cbo_ma_doc_gia.ValueMember = V_GD_PHIEU_MUON.ID_DOC_GIA;

            m_cbo_ten_doc_gia.DataSource = v_ds_gd_phieu_muon.V_GD_PHIEU_MUON;
            m_cbo_ten_doc_gia.DisplayMember = V_GD_PHIEU_MUON.HO_TEN;
            m_cbo_ten_doc_gia.ValueMember = V_GD_PHIEU_MUON.ID_DOC_GIA;
        }

        private void format_controls()
        {
            CControlFormat.setFormStyle(this);
            set_define_events();
        }
        private void us_object_to_form()
        {
            load_data_to_combobox();
            m_txt_ma_phieu.Text = m_us_v_gd_phieu_muon.strMA_PHIEU;
            m_cbo_ten_doc_gia.SelectedValue = m_us_v_gd_phieu_muon.dcID_DOC_GIA;
            m_cbo_ma_doc_gia.SelectedValue = m_us_v_gd_phieu_muon.dcID_DOC_GIA;
            m_dat_ngay_het_han.Value = m_us_v_gd_phieu_muon.datNGAY_HET_HAN;
            m_dat_ngay_muon.Value = m_us_v_gd_phieu_muon.datNGAY_MUON;
        }
        private void form_to_us_object()
        {
            m_us_gd_phieu_muon.dcID = m_us_v_gd_phieu_muon.dcID;
            m_us_gd_phieu_muon.strMA_PHIEU = m_txt_ma_phieu.Text;
            m_us_gd_phieu_muon.dcID_DOC_GIA =CIPConvert.ToDecimal(m_cbo_ten_doc_gia.SelectedValue);
            m_us_gd_phieu_muon.datNGAY_MUON=m_dat_ngay_muon.Value;
            m_us_gd_phieu_muon.datNGAY_HET_HAN=m_dat_ngay_het_han.Value;
        }
        private void set_inital_form_load()
        {
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    enable_combobox(false);
                    us_object_to_form();
                    break;
                case DataEntryFormMode.InsertDataState:
                    load_data_to_combobox();
                    enable_combobox(true);
                    break;
                default:
                    break;
            }
        }
        private void set_define_events()
        {
            this.Load +=new EventHandler(f009_V_GD_PHIEU_MUON_DE_Load);
            m_cmd_update.Click += new EventHandler(m_cmd_update_Click);
            m_cmd_exit.Click += new EventHandler(m_cmd_exit_Click);
        }
        private bool check_validate_data_is_ok()
        {
            if(!CValidateTextBox.IsValid(m_txt_ma_phieu,DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
            if(m_dat_ngay_muon.Value>m_dat_ngay_het_han.Value)
            {
                BaseMessages.MsgBox_Error("Ngày Mượn phải trước Ngày Hết Hạn!");
                return false;
            }
            return true;
        }
        private void save_data()
        {
            if (check_validate_data_is_ok() == false) return;
            form_to_us_object();
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    m_us_gd_phieu_muon.Update();
                    break;
                case DataEntryFormMode.InsertDataState:
                    m_us_gd_phieu_muon.Insert();
                    break;
                default:
                    break;
            }
            BaseMessages.MsgBox_Infor("Dữ liệu đã được cập nhât!");
            this.Close();
        }

        #endregion

        #region Event
        private void f009_V_GD_PHIEU_MUON_DE_Load(object sender, EventArgs e)
        {
            try
            {
                set_inital_form_load();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_update_Click(object sender, EventArgs e)
        {
            try
            {
                save_data();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_exit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        #endregion
    }
}
